package com.googlecode.adaboost.trainer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import com.googlecode.adaboost.exception.AdaBoostException;

public class AdaBoostConfiguration {

	private TrainingType trainingType;
	private int maxEpochs;
	private double desiredError;
	private double epsilon;
	private Properties properties;

	public AdaBoostConfiguration(String propertiesFile)
			throws AdaBoostException, FileNotFoundException, IOException {

		properties = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(propertiesFile);
			properties.load(fis);
		} finally {
			fis.close();
		}

		trainingType = properties.getProperty("global.training-type")
				.toLowerCase().equals("binary") ? TrainingType.BINARY_ADABOOST
				: TrainingType.REAL_ADABOOST;

		// if (properties.getProperty("max-epochs") == null)
		// throw new AdaBoostException(
		// "Required parameter 'max-epochs' not found (see "
		// + "configuration).");
		maxEpochs = Integer.parseInt(properties
				.getProperty("global.max-epochs"));
		if (maxEpochs <= 0) {
			throw new AdaBoostException(
					"max-epochs must be greater than 0.0 under real mode");
		}

		// if (properties.getProperty("desired-error") == null)
		// throw new AdaBoostException(
		// "Required parameter 'desired-error' not found (see "
		// + "configuration).");
		desiredError = Double.parseDouble(properties
				.getProperty("global.desired-error"));

		// if (properties.getProperty("epsilon") == null)
		// throw new AdaBoostException(
		// "Required parameter 'epsilon' not found (see "
		// + "configuration).");
		epsilon = Double.parseDouble(properties.getProperty("global.epsilon"));

		if (epsilon == 0.0 && trainingType == TrainingType.REAL_ADABOOST) {
			throw new AdaBoostException(
					"global.epsilon must be greater than 0.0 under real mode");
		}

	}

	public int getMaxEpochs() {
		return maxEpochs;
	}

	public void setMaxEpochs(int maxEpochs) {
		this.maxEpochs = maxEpochs;
	}

	public double getDesiredError() {
		return desiredError;
	}

	public void setDesiredError(double desiredError) {
		this.desiredError = desiredError;
	}

	public double getEpsilon() {
		return epsilon;
	}

	public void setEpsilon(double epsilon) {
		this.epsilon = epsilon;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public TrainingType getTrainingType() {
		return trainingType;
	}

	public void setTrainingType(TrainingType trainingType) {
		this.trainingType = trainingType;
	}

}
